<?php
error_reporting(E_ALL);
require __DIR__.'/../Connect.php';

use PhpAmqpLib\Message\AMQPMessage;

class Task
{

    //public $exchange = 'direct_exchange';



    public function send()
    {
        $connect = Connect::getInstance();
        $channel = $connect->channel();

        $args = new \PhpAmqpLib\Wire\AMQPTable(
            [
                'x-dead-letter-exchange'    => 'dead1',
                //'x-message-ttl'             => 50000, //消息在原队列存活时间毫秒 这个事件过后就会转移到死信队列
                'x-dead-letter-routing-key' => 'dead1'
            ]
        );

        $channel->exchange_declare('dead1','topic',false,false,false);
        $channel->queue_declare('dead1.all', false, false, false, false);
        $channel->queue_bind('dead1.all', 'dead1', 'dead1');


        $channel->set_ack_handler(function (AMQPMessage $message){
            echo 'ack' . $message->getBody() . PHP_EOL;
        });
        //nack callback function
        $channel->set_nack_handler(function (AMQPMessage $message){
            echo 'nack' . $message->getBody() .PHP_EOL;
        });

        //$channel->exchange_declare($this->exchange,'fanout',false,true);
        $channel->queue_declare('no_consume1', false,false,false,false,false,$args);
        $msg = 'Hello world.'.date('Y-m-d H:i:s').rand(1,100);
        $msgObj = new AMQPMessage($msg, ['delivery_mode' => AMQPMessage::DELIVERY_MODE_PERSISTENT]);

        $channel->confirm_select(false);
        $channel->basic_publish($msgObj,'','no_consume1');
        $channel->wait_for_pending_acks_returns(5);

        $channel->close();
        $connect->close();
        echo '信息发送成功'.PHP_EOL;
    }
}
(new Task())->send();
